/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package midpmpc;

import midpmpc.ui.MenuOption;
import org.j4me.logging.Log;
import org.j4me.ui.Menu;


/**
 *
 * @author vvs
 */
public class LibraryView extends FileListView{
    Menu menu;

    CallbackMenuItem playCmd,addCmd;
    public LibraryView(Mpc parent) {
        super(parent);
        setTitle("Library");
        setMenuText("add", "back");
        menu=new Menu("action", this);
    }
    public String formatFile(FileInfo f)
    {
        String tn;
        int sp=f.file.lastIndexOf('/');
        if (sp==-1)
            tn= f.file;
        else
            tn=f.file.substring(sp+1);
        if (f.isDir)
            tn=tn+"/";
        return tn;
    }
    public void keyPressed(int key)
    {
        if (key==FIRE)
        {
            if (files[getSelected()].isDir)
            {
//                deleteAll();
//                append(new MenuOption("please wait..."));
//                repaint();
                setFiles(MpdConnection.sendCmd("lsinfo \""+currentFile()+"\"").toFileList());
            }
        }
        else if (key==MENU_LEFT)
            MpdConnection.sendCmd("add \""+currentFile()+"\"");
        else if (key==MENU_RIGHT)
        {
            try
            {
            String currFile=currentFile();
            Log.warn("cf "+currFile);
            if (currFile.lastIndexOf('/')==-1)
            {
                super.keyPressed(key);
                return;
            }
            String currDir=currFile.substring(0, currFile.lastIndexOf('/'));
            Log.warn(currDir);
            if (currDir.lastIndexOf('/')==-1)
            {
                setFiles(MpdConnection.sendCmd("lsinfo").toFileList());
                repaint();
                return;
            }
            String upDir=currDir.substring(0, currDir.lastIndexOf('/'));

            Log.warn(upDir);
            setFiles(MpdConnection.sendCmd("lsinfo \""+upDir+"\"").toFileList());
            }
            catch (Exception e)
            {
                Log.warn(null, e);
            }
        }
        else
        {
            super.keyPressed(key);
            return;
        }
        repaint();
    }
    public void showNotify()
    {
        setFiles(MpdConnection.sendCmd("lsinfo").toFileList());
        super.showNotify();

    }

}
